/**
 * 应用数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.AppGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	initComponent: function(){
		var gridStructure = [{  
			name: 'appId',
			hidden: true
		},{
			name: 'appCode',  
			header: "应用编号",
			sortable: true,
			width: 50
		},{
			name: 'appName',  
			header: "应用名称",
			sortable: true,
			width: 50
		},{
			name: 'appTypeLabel',  
			header: "应用类型",
			width: 25
		},{
			name: 'appUrl',  
			header: "应用访问地址",
			width: 100
		},{
			type: 'boolean',
			name: 'isActive',  
			header: "是否启用",
			width: 25
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'appCode';
		this.keyField = 'appId';
		
		this.deleteMsg = '确认要删除您选择的应用吗？<br/><font color="red">注意</font>：删除应用将删除该应用下的所有模块和功能。';
		this.deleteMsgBoxWidth = 370;
		
		Ext.ux.grid.AppGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(appGrid){
				var record = appGrid.getSelectionModel().getSelected();
				if(record != null){
					var appId = record.get('appId');
					this.showAppInfo(appId);
				}
			});
		}
		
		
		this.on('addRecord', function(){
			this.addApp();
		});
		
		this.on('updateRecord', function(appId){
			this.showAppInfo(appId);
		});
		
		this.on('deleteRecord', function(grid, appIds){
			this.deleteApp(appIds);
		});
	},
	
	addApp: function(){
		var win = new Ext.ux.org.acapp.AppDetailWindow({id:'appDetailWindow', title:'新建应用'});
		win.show();
	},
	
	showAppInfo: function(appId){
		var win = new Ext.ux.org.acapp.AppDetailWindow({id:'appDetailWindow', title:'修改应用', appId:appId});
		win.showAppInfo();
	},
	
	deleteApp: function(appIds) {
		Ext.Ajax.request({
			url: 'ac-application!delete.action',
			success: function(){
				Ext.getCmp('appTree').refreshOrg();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除机构失败！');
			},
			method: 'POST',
			params:{
				appIds: appIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	}
});